//
//  Labs_LLDB_DemoVC.swift
//  APSwift
//
//  Created by jianbo.pei on 2022/3/20.
//

import UIKit

class Labs_LLDB_Manager: NSObject {
    public static var share = Labs_LLDB_Manager()
    
    public weak var demoVC: Labs_LLDB_Demo1VC?
    public weak var notPresentedNavC: UINavigationController?

}

class Labs_LLDB_Demo1VC: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
        let num = 99
        let str = "jianbo.pei"
        print("num is \(num), str is \(str)")
        
        let arr: [Int: String] = [1: "1", 3: "3", 5: "5"]
        if let item = arr[7] {
            print("item is \(item)")
        } else {
            print("error")
        }
        
        let bt = UIButton(type: .custom)
        view.addSubview(bt)
        bt.backgroundColor = .red
        bt.frame = CGRect(x: 50, y: 200, width: 200, height: 40)
        bt.addTarget(self, action: #selector(action), for: .touchUpInside)
    }
    
    @objc private func action() {
        print("123 --action \(String(describing: Labs_LLDB_Manager.share.demoVC))")

        let vc = Labs_LLDB_Presented_DemoVC()
        Labs_LLDB_Manager.share.demoVC = self
        Labs_LLDB_Manager.share.notPresentedNavC = navigationController
        let nav = UINavigationController(rootViewController: vc)
        nav.modalPresentationStyle = .custom
        present(nav, animated: true)
    }
    
    public func sayBack() {
        print("123 -- 回来了 \(String(describing: Labs_LLDB_Manager.share.demoVC))")
    }


}

class Labs_LLDB_Presented_DemoVC: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    
        view.backgroundColor = .gray
        
        let bt = UIButton(type: .custom)
        view.addSubview(bt)
        bt.backgroundColor = .purple
        bt.frame = CGRect(x: 50, y: 200, width: 200, height: 40)
        bt.addTarget(self, action: #selector(action), for: .touchUpInside)
    }
    
    @objc private func action() {
//        navigationController?.dismiss(animated: true)
//        dismiss(animated: true)
        let demo1VC = Labs_LLDB_Presented_Demo1VC()
        navigationController?.pushViewController(demo1VC, animated: true)
    }


}

class Labs_LLDB_Presented_Demo1VC: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    
        view.backgroundColor = .blue
        
        let bt = UIButton(type: .custom)
        view.addSubview(bt)
        bt.backgroundColor = .purple
        bt.frame = CGRect(x: 50, y: 200, width: 200, height: 40)
        bt.addTarget(self, action: #selector(action), for: .touchUpInside)
    }
    
    @objc private func action() {
        
        if let demoVC = Labs_LLDB_Manager.share.demoVC {
            dismiss(animated: true) {
                Labs_LLDB_Manager.share.demoVC = nil
                print("123 -- Labs_LLDB_Manager.share.demoVC = nil")
            }
            demoVC.sayBack()
            return
        }
        if let nav = navigationController {
            print("123 -- has nav \(nav.viewControllers)")
            if let presentingVC = nav.presentingViewController {
                print("123 -- presentingVC nav \(presentingVC)")
                dismiss(animated: true)
//                nav.dismiss(animated: true)
                return
            }
            nav.popViewController(animated: true)
        }
//        navigationController?.dismiss(animated: true)
//        dismiss(animated: true)
    }


}
